//
//  WZPThemePictureView.m
//  WZPItem
//
//  Created by 王王喆平 on 16/7/14.
//  Copyright © 2016年 UI. All rights reserved.
//

#import "WZPThemePictureView.h"
#import "WZPThemeItem.h"
#import <UIImageView+WebCache.h>
#import <DALabeledCircularProgressView.h>
#import <SDImageCache.h>
#import "WZPSeeBigPictureController.h"

@interface WZPThemePictureView ()
@property (weak, nonatomic) IBOutlet UIImageView *pictureView;
@property (weak, nonatomic) IBOutlet UIButton * bigBtn;
@property (weak, nonatomic) IBOutlet UIImageView *gifView;
@property (weak, nonatomic) IBOutlet DALabeledCircularProgressView *progressView;


@end

@implementation WZPThemePictureView


-(void)awakeFromNib{
    [super awakeFromNib];
    
    _progressView.roundedCorners = 10;
    _progressView.progressLabel.textColor = [UIColor whiteColor];
    

}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    WZPSeeBigPictureController * ws = [[WZPSeeBigPictureController alloc]init];
    ws.item = self.item;
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:ws animated:YES completion:nil];
    
    

}
-(void)setItem:(WZPThemeItem *)item{

   
    [super setItem:item];

    _progressView.progress = 0;
    _progressView.progressLabel.text = @"0%";
    if (item.is_gif) {
        
        [_pictureView sd_setImageWithURL:[NSURL URLWithString:item.image1]placeholderImage:nil options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize){
        
            CGFloat progress = 1.0 * receivedSize / expectedSize;
            
            if (progress > 0) {
                
                _progressView.progress = progress;
                
                // 10%
                _progressView.progressLabel.text = [NSString stringWithFormat:@"%.1f%%",progress * 100];
            }
            
            
        } completed:nil];

    }else{
    // 对图片进行处理
    UIImage * image = [[SDImageCache sharedImageCache]imageFromDiskCacheForKey:item.image0];
    
    [_pictureView sd_setImageWithURL:[NSURL URLWithString:item.image0]placeholderImage:image options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            // 监听进度
        CGFloat progress = 1.0 * receivedSize / expectedSize;
        if (progress > 0) {
            _progressView.progress = progress;
            _progressView.progressLabel.text = [NSString stringWithFormat:@"%.1f%%",progress * 100];
        }
           
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            // 当不是大图时不进行处理
            if (!item.is_bigPicture) return ;
        
            CGFloat pictureW = WZPScreenW - 20;
            CGFloat pictureH = pictureW / item.width * item.height;
            CGRect  frame = CGRectMake(0, 0, pictureW, pictureH);
            // 进行图片裁剪
            // 开启图形上下文
            UIGraphicsBeginImageContextWithOptions(frame.size, NO, 0);
            // 绘制
            [image drawInRect:frame];
            // 获取图片
            image = UIGraphicsGetImageFromCurrentImageContext();
            // 关闭上下文
            UIGraphicsEndImageContext();
            
            // 保存图片
            [[SDImageCache sharedImageCache] storeImage:image forKey:item.image0];
            _pictureView.image = image;
            
        }];
    
    _gifView.hidden = !item.is_gif;
    
    _bigBtn.hidden = !item.is_bigPicture;
    // 设置图片模式
    if(item.is_bigPicture){
        _pictureView.contentMode = UIViewContentModeTop;
        _pictureView.clipsToBounds = YES;
    
    }else{
        _pictureView.contentMode = UIViewContentModeScaleToFill;
        _pictureView.clipsToBounds = NO;
        
    }
    }
}
@end
